import React, {useEffect, useState} from 'react';
import ReactDOM from 'react-dom';
// eslint-disable-next-line
import './index.css';
import Layout from './Layout';
import * as serviceWorker from './serviceWorker';
import {Route, BrowserRouter, Switch, Redirect} from 'react-router-dom';
import LoginLayout from "./MyLayout/LoginLayout";
import MainLayout from "./MyLayout/MainLayout";
import ReleaseLayout from "./MyLayout/ReleaseLayout";

// 应用的位置
const App = () => {
    const [isLoginEd, setIsLoginEd] = useState(false);
    useEffect(()=>{
        let userInfoIsLoginEd = localStorage.getItem("userInfo:isLoginEd");
        if (userInfoIsLoginEd!==null && userInfoIsLoginEd === "true"){
            setIsLoginEd(true);
        }
    })
    return <BrowserRouter>
        <Switch>
            {/* 不需要进行权限控制的路由 */}
            <Route exact path="/release" component={ReleaseLayout}/>
            {/* 需要进行权限控制的路由 */}
            {isLoginEd && <Route exact path="/new" component={Layout}/>}
            {!isLoginEd && <Route exact path="/login" component={LoginLayout}/>}
            {isLoginEd && <Route path="/main" component={MainLayout}/>}
            {isLoginEd && <Redirect to="/main"/>}
            {!isLoginEd && <Redirect to="/login"/>}
        </Switch>
    </BrowserRouter>
}


ReactDOM.render(
    <App/>,
    document.getElementById('root')
);

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
// serviceWorker.unregister();
serviceWorker.register();
